package org.nobody.mallb2c.mallorder.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.nobody.common.to.mq.SeckillOrderTo;
import org.nobody.mallb2c.mallorder.service.OrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author 3489309323@qq.com
 * @create 2020-09-29-0:40
 */
@Slf4j
@RabbitListener(queues = "order.seckill.order.queue")
@Component
public class OrderSeckillListener {
  @Autowired
  OrderService orderService;

  @RabbitHandler
  public void listener(SeckillOrderTo entity, Channel channel, Message message) throws IOException {
    log.info("准备创建秒杀单的详细信息...");
    try {
      orderService.createSeckillOrder(entity);
      channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    } catch (Exception e) {
      channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
    }
  }
}
